package com.hsylient.javase.design.pattern.observer;

public class ObserverPatternExample {
    public static void main(String[] args) {
        NewsPublisher newsPublisher = new NewsPublisher();

        Observer subscriber1 = new NewsSubscriber("Subscriber 1");
        Observer subscriber2 = new NewsSubscriber("Subscriber 2");
        Observer subscriber3 = new NewsSubscriber("Subscriber 3");

        newsPublisher.registerObserver(subscriber1);
        newsPublisher.registerObserver(subscriber2);
        newsPublisher.registerObserver(subscriber3);

        newsPublisher.publishNews("重要新闻: 今天是假日!");

        newsPublisher.removeObserver(subscriber2);

        newsPublisher.publishNews("特别报道: 天气晴朗。");

        // 输出示例：
        // Subscriber 1 收到新闻: 重要新闻: 今天是假日!
        // Subscriber 2 收到新闻: 重要新闻: 今天是假日!
        // Subscriber 3 收到新闻: 重要新闻: 今天是假日!
        // Subscriber 1 收到新闻: 特别报道: 天气晴朗！
        // Subscriber 3 收到新闻: 特别报道: 天气晴朗！
    }
}